package com.plan.controller.user;

import java.io.*;
import java.net.Socket;

public class TcpClientTest {

    public static void main(String[] args) {
        // 花生壳提供的地址和端口
        String hostname = "42647bl98hv0.vicp.fun";
        int port = 23997;

        try (Socket socket = new Socket(hostname, port)) { // 创建Socket连接
            System.out.println("成功连接到服务器：" + hostname + ":" + port);

            // 获取输出流，发送消息给服务器
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream, true);
            writer.println("Hello from TCP Client!"); // 发送的消息可以按实际需求修改

            // 获取输入流，接收服务器响应
            InputStream inputStream = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String response;
            while ((response = reader.readLine()) != null) {
                System.out.println("收到服务器响应: " + response);
            }

        } catch (IOException e) {
            System.err.println("连接失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}